// var express = require('express');
// var router = express.Router();
// var sql =require("../db/my");

// /* GET users listing. */
// router.get('/user', function(req, res, next) {
//     // var bbb = req.query.callback;

//     sql('select *from user').then(data=>{
//       // data =JSON.stringify(data);
//       // res.send(`${bbb}(${data})`)
//       res.send(data)
//     //   res.render('index', { user:data,title: 'Express' });

//     })
//   });

//   module.exports = router;
// 传入第三方的邮箱
const nodemailer = require("nodemailer");

const { json } = require('express');
var express = require('express');
var router = express.Router();

let sql = require('../db/my.js')




  

 




/* GET users listing. */
// router.get('/', function(req, res, next) {
//   res.send('api');
// });
// router.get('/user', function(req, res, next) {

//   sql('select * from user')
//   .then(data=>{
//     console.log(data);
//     res.send(data)
//   })

//   }),
router.post('/user', function (req, res, next) {
  // console.log(req.body);
  var info = req.body;
  sql(`select * from user where name='${info.username}'`).then(data => {
    // console.log(data);
    if (data.length > 0) {
      // console.log(data[0].age)
      if (data[0].age == info.password) {
        sql(`select * from cart where name='${info.username}'`).then(data => {
          res.send({
            code: 200,
            msg: '登陆成功',
            // 成功请求cart表
            // shop: data
            shop: data
          });
        });

      } else {
        res.send({
          code: 300,
          msg: '输入正确密码'
        })
      }

    } else {
      res.send({
        code: 400,
        msg: '请去注册'
      })
    }
  })

});

// 注册
router.post('/usy', function (req, res, next) {
  // console.log(req.body);
  var info = req.body;

  sql(`select * from user where name='${info.username}'`).then(data => {
    console.log(data);
    if (data.length > 0) {
      res.send({
        code: 200,
        msg: '已有账号请登录'
      })
    }
    else {
      sql(`insert into user values('${info.username}','${info.password}','${info.youxiangs}')`).then(result => {
        // console.log(result);
        if (result) {
          res.send({
            code: 300,
            msg: '注册成功!'
          })
        }
      });
    }
  })

});
// 头像
router.post('/file', function (req, res, next) {
  // console.log(req.body.file)
  var file = JSON.stringify(req.body.file);
  var infos = req.body.username;
  sql(`select * from file where name = '${infos}'`).then(data => {
    if (data.length == 0) {
      // 无同人
      sql(`insert into file values ('${infos}','{"file":${file}}')`).then(data => {
        // console.log(data)
        if (data) {
          res.send({
            code: 200,
            msg: '保存成功'
          })
        } else {
          res.send({
            code: 400,
            msg: '保存失败'
          })
        }
      })
    }
    //     else {
    //       res.send({
    //         code: 200,
    //         msg: '保存失败'
    //       })
    //     }
    //   })
    // })
    else {
      sql(`update file set file = '{"file":${file}}'
       where name = '${infos}'`).then(data => {
        // console.log(data)
        if (data) {
          res.send({
            code: 200,
            msg: '保存成功'

          })
        } else {
          res.send({
            code: 400,
            msg: '保存失败'
          })
        }
      })
    }
  })
})

// 找回密码
router.post('/email', function (req, res, next) {
  // console.log(req.body);
  var info = req.body;

  sql(`select * from user where email='${info.youxiangs}'and name = '${info.username}'`).then(data => {
    console.log(data);
    if (data.length > 0) {
      res.send({
        code: 200,
        password: data,
        msg:data
      })
    }
    else {
      res.send({
        code: 400,
        msg: '请输入正确邮箱或用户名'
      })
    }
  })

});
// 评论
router.post('/commentes', function (req, res, next) {
  // console.log(req.body);
  var info = req.body;
  var coments=req.body.main;
  console.log(req.body.main)
  sql(`select * from latest where pic='${info.some.pic}'`).then(data => {
    sql(`update latest set coments='{"coments":${coments}}' where pic='${info.some.pic}'`).then(result => {
      // console.log(result);
      if (result) {
        res.send({
          code: 300,
          msg: '注册成功!'
        })
      }
    });
  })

});

// 获取内容
router.get('/books', function (req, res, next) {

  sql('select * from data')
    .then(data => {
      // console.log(data);
      res.send(data)
    })
}),


// 获取书单
router.get('/list', function (req, res, next) {

  sql('select * from list')
    .then(data => {
      // console.log(data);
      res.send(data)
    })
}),

// 轮播
router.get('/lbs', function (req, res, next) {

  sql('select * from lbs')
    .then(data => {
      // console.log(data);
      res.send(data)
    })
}),

// 获取首页单个
router.get('/sy', function (req, res, next) {

  sql('select * from sy')
    .then(data => {
      // console.log(data);
      res.send(data)
    })
}),

// 分类
router.get('/classification', function (req, res, next) {

  sql('select * from classification')
    .then(data => {
      // console.log(data);
      res.send(data)
    })
}),

// 精选
router.get('/latest', function (req, res, next) {
  sql('select * from latest')
    .then(data => {
      console.log(data);
      res.send(data)
    })
}),

// 本周最热
router.get('/hottest',(req,res)=>{
  sql('select * from hottest').then(
    data=>{
      // console.log(data)
      res.send(data)
    }
  )
})

// 最多收藏
router.get('/collection',(req,res)=>{
  sql('select * from collection').then(data=>{
    // console.log(data)
    res.send(data)
  })
})

// 推荐
router.get('/recommend',(req,res)=>{
  sql('select * from recommend').then(data=>{
    // console.log(data)
    res.send(data)
  })
})

// 书
router.get('/bookes',(req,res)=>{
  sql('select * from books').then(data=>{
    // console.log(data)
    res.send(data)
  })
})

// email
router.post('/toemail', function (req, res, next) {
  // console.log(req.body);
  var info = req.body;

  sql(`select * from user where email='${info.youxiangs}'and age = '${info.password}'`).then(data => {
    // console.log(data);
    if (data.length > 0) {
      res.send({
        code: 200,
      })
    }
    else {
      res.send({
        code: 400,
        msg: '请输入正确邮箱或用户名'
      })
    }
  })
});


router.get('/image', function (req, res, next) {
  
  sql('select * from buying')
    .then(data => {
      console.log(data);
      res.send(data)
    })
}),

// 搜索内容
router.post('/Search', async(req, res, next)=>{
  // console.log(req.body.search)
  var search = req.body.search;
  var data = await sql(`select * from generaltable where title like '${search}%' `).then(data => {
    if (data) {
        res.send({
            data:data,
            code: 200,
            msg: '查找成功'
        })
    }
    else {
        res.send({
            code: 500,
            msg: '查找失败'
        })
    }
})
})
// 评分内容
router.get('/Searchping', function (req, res, next) {

  sql('select * from generaltable  where score>9.0  ORDER BY score desc')
    .then(data => {
      // console.log(data);
      res.send(data)
    })
}),

// 热更
router.get('/Searchhot',async(req,res)=>{

  var hot = await sql(`select *from generaltable where state='连载'`);
  if(hot){
    res.send({
      code: 200,
      data: hot
  })
  }
  else{
    res.send({
      code: 500,
      data:"失败"
  })
  }
})
// 完结
router.get('/Searchlast',async(req,res)=>{
  var hot = await sql(`select *from generaltable where state='完结'`);
  if(hot){
    res.send({
      code: 200,
      data: hot
  })
  }
  else{
    res.send({
      code: 500,
      data:"失败"
  })
  }
})
// 分类内容
router.post('/classification', async(req, res, next)=>{
  // console.log(req.body.search)
  var search = req.body.search;
  var data = await sql(`select * from generaltable where category='${search}' `).then(data => {
    if (data) {
        res.send({
            data:data,
            code: 200,
            msg: '查找成功'
        })
    }
    else {
        res.send({
            code: 500,
            msg: '查找失败'
        })
    }
    
})
})
  // 购物车缓存
  router.post('/incart', function (req, res, next) {
    // console.log(req.body.shop)
    var shop = JSON.stringify(req.body.shop);
    var collection = req.body.Collection ;
    var infos = req.body.username;
    console.log(collection)
   console.log(req.body.Collection)
    sql(`select * from cart where name = '${infos}'`).then(data => {
      if (data.length == 0) {
        // 无同人
        sql(`insert into cart values ('${infos}','{"shop":${shop}}','{"collection":${collection}}')`).then(data => {
          console.log(data)
          if (data) {
            res.send({
              code: 200,
              msg: '保存成功'

            })
          } else {
            res.send({
              code: 400,
              msg: '保存失败'
            })
          }
        })
      }
      //     else {
      //       res.send({
      //         code: 200,
      //         msg: '保存失败'
      //       })
      //     }
      //   })
      // })
      else {
        sql(`update cart set cart = '{"shop":${shop}}', collection='{"collection":${collection}}'
         where name = '${infos}'`).then(data => {
          console.log(data)
          if (data) {
            res.send({
              code: 200,
              msg: '保存成功'

            })
          } else {
            res.send({
              code: 400,
              msg: '保存失败'
            })
          }
        })
      }
    })
  }) 

// 邮箱验证  let em = req.query.youxiangs ;
router.get('/sms',async(req,res,next)=>{
  // console.log(req.query.youxiangs)
  // 3322975967@qq.com
  let em = req.query.youxiangs ;
  var sms;
  var arr = [];
  // var arr1 =[e,1,2,3,4,5,6,7,8,9,A,B,C,D];
  for (let i = 0; i < 4; i++) {
    // Math.ceil(Math.random()*(max-min)+min)js中向下取整的随机整数的一个公式(0-10)
    var randNum = Math.ceil(Math.random() * 9);
    arr.push(randNum);
  }
  sms = arr.join("");

  let transporter = nodemailer.createTransport({
    // QQ邮箱的主机号
    host: "smtp.qq.com",
    // QQ邮箱的端口号，默认465
    port: 465,
    secure: true,
    //发送方的邮箱配置
    auth: {
      //发送方的邮箱
      user: "1761830309@qq.com", 
      //发送人邮箱的授权码，这里可以通过qq邮箱获取 并且不是唯一的
      pass: "iummemztihszbhef",
    }
  });
  let info = await transporter.sendMail({
    from: '"[天天看]验证码" 1761830309@qq.com',
    to: em,
    subject: "[天天看]验证码 ",
    html: `[天天看]小哥哥，小姐姐，你们的验证码<h1 style='color:skyblue'>'${sms}'</h1>,请在十分钟内验证。如非本人操作请勿忽略。`,
  })
  if(info){
    res.send({
      code: 200,
      sms: sms
    })
  }else{
    res.send({
      code: 500,
      sms: "验证码发送错误"
    })
  }
  console.log(info);
})

module.exports = router;